/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl;

import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.ControlFlowSegment;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionPath;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocolElement;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionprotocolsPackage;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Termination;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Xor;

import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.util.InteractionprotocolsValidator;
import java.util.Map;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.plugin.EcorePlugin;
import org.eclipse.emf.ecore.util.EObjectValidator;



/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Xor</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class XorImpl extends CFSNoSyncImpl implements Xor {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected XorImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return InteractionprotocolsPackage.Literals.XOR;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public boolean interactionPathTerminations(DiagnosticChain diagnostics, Map context) {
		// TODO: implement this method
		// -> specify the condition that violates the invariant
		// -> verify the details of the diagnostic, including severity and message
		// Ensure that you remove @generated or mark it @generated NOT
		if (firstXorPathLevel()) {
			if (diagnostics != null) {
				diagnostics.add
					(new BasicDiagnostic
						(Diagnostic.ERROR,
						 InteractionprotocolsValidator.DIAGNOSTIC_SOURCE,
						 InteractionprotocolsValidator.XOR__INTERACTION_PATH_TERMINATIONS,
						 EcorePlugin.INSTANCE.getString("_UI_GenericInvariant_diagnostic", new Object[] { "At least on interaction path must not contain a Termination element", EObjectValidator.getObjectLabel(this, context) }), //$NON-NLS-1$ //$NON-NLS-2$
						 new Object [] { this }));
			}
			return false;
		}
		return true;
	}

	private boolean firstXorPathLevel() {
		
		
		int j = this.getInteractionPath().size();
		
		for(int i=0;this.getInteractionPath().size()>i;i++) {
			
			if(xorPathLevel(this.getInteractionPath().get(i))) j--;
			
		}
		if(j==0) return true;
		
		return false;
	}
	
	private boolean xorPathLevel(InteractionPath ip) {
		
		ControlFlowSegment ie = null;
		
		if(ip.getElement().size()==0) return false;
		else for(int j=0;j<ip.getElement().size();j++) {
				if(!(ip.getElement().get(j).getClass()==(TerminationImpl.class))) {
			
					if (ip.getElement().get(j) instanceof ControlFlowSegment){
						ie = (ControlFlowSegment) ip.getElement().get(j);
				
						for(int i=0;ie.getInteractionPath().size()>i;i++) {
					
								if(xorPathLevel(ie.getInteractionPath().get(i))) return true;
					
						}
					}
				}	
				else return true;
		}
		
		return false;
		
	}
	
} //XorImpl
